/* Turska UI library
 * Copyright 2003-2008 Jetro Lauha
 * All rights reserved.
 * Web: http://iki.fi/jetro/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of EITHER:
 *   (1) The GNU Lesser General Public License as published by the Free
 *       Software Foundation; either version 2.1 of the License, or (at
 *       your option) any later version. The text of the GNU Lesser
 *       General Public License is included with this library in the
 *       file LICENSE-LGPL.txt.
 *   (2) The BSD-style license that is included with this library in
 *       the file LICENSE-BSD.txt.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
 *
 * $Id: UISlider.cpp,v 1.2 2008/03/27 11:44:29 tonic Exp $
 * $Revision: 1.2 $
 */

#include "UI.h"


UISlider::UISlider(INT x, INT y, UIImage *image, UIImage *thumbImage,
                   UINT orientation, double value,
                   INT minPosition, INT maxPosition) :
    UIButton(x, y, image, NULL, thumbImage, NULL, NULL, 0, 0, 0, 0, 0),
    mMinPosition(minPosition), mMaxPosition(maxPosition), mOrientation(orientation)
{
    setSymbolImageFlags(UIImage::COLOR);

    if (value < 0) value = 0;
    if (value > 1) value = 1;

    if (thumbImage != NULL)
    {
        if (mOrientation == HORIZONTAL)
        {
            if (mMinPosition == -1)
                mMinPosition = thumbImage->getWidth() / 2;
            if (mMaxPosition == -1)
                mMaxPosition = getWidth() - thumbImage->getWidth() / 2;
        } else if (mOrientation == VERTICAL)
        {
            if (mMinPosition == -1)
                mMinPosition = thumbImage->getHeight() / 2;
            if (mMaxPosition == -1)
                mMaxPosition = getHeight() - thumbImage->getHeight() / 2;
        }
    }

    setValue(value);
}

    
void UISlider::setPosition(INT position)
{
    mPosition = position;
    if (mPosition < mMinPosition) mPosition = mMinPosition;
    if (mPosition > mMaxPosition) mPosition = mMaxPosition;
}


void UISlider::setValue(double value)
{
    if (value < 0) value = 0;
    if (value > 1) value = 1;
    setPosition((INT)(value * (mMaxPosition - mMinPosition + 1) + mMinPosition));
}


double UISlider::getValue()
{
    INT relativePosition = mPosition - mMinPosition;
    double value = (double)relativePosition / (mMaxPosition - mMinPosition);
    return value;
}


// TODO - refactorize away the copy-paste between onPointerDown/Up/Move


void UISlider::onPointerDown(INT x, INT y)
{
    UIButton::onPointerDown(x, y);

    INT newPosition;
    if (mOrientation == HORIZONTAL)
        newPosition = x - getX();
    else
        newPosition = y - getY();
    INT oldPosition = getPosition();
    setPosition(newPosition);
    if (oldPosition != newPosition)
    {
        // report action event on value changes from pointer move events
        EventListenerSet::iterator it = mEventListeners.begin();
        EventListenerSet::iterator itend = mEventListeners.end();
        for (; it != itend; ++it)
            (*it)->onAction(this);
    }
}


void UISlider::onPointerUp(INT x, INT y)
{
    if (isPressed() && isInside(x, y))
    {
        INT newPosition;
        if (mOrientation == HORIZONTAL)
            newPosition = x - getX();
        else
            newPosition = y - getY();
        INT oldPosition = getPosition();
        setPosition(newPosition);
        if (oldPosition != newPosition)
        {
            // report action event on value changes from pointer move events
            EventListenerSet::iterator it = mEventListeners.begin();
            EventListenerSet::iterator itend = mEventListeners.end();
            for (; it != itend; ++it)
                (*it)->onAction(this);
        }
    }
    UIButton::onPointerUp(x, y);
}


void UISlider::onPointerMove(INT x, INT y)
{
    if (isPressed())
    {
        INT newPosition;
        if (mOrientation == HORIZONTAL)
            newPosition = x - getX();
        else
            newPosition = y - getY();
        INT oldPosition = getPosition();
        setPosition(newPosition);
        if (oldPosition != newPosition)
        {
            // report action event on value changes from pointer move events
            EventListenerSet::iterator it = mEventListeners.begin();
            EventListenerSet::iterator itend = mEventListeners.end();
            for (; it != itend; ++it)
                (*it)->onAction(this);
        }
        setPosition(newPosition);
    }
    UIComponent::onPointerMove(x, y);
}


void UISlider::draw()
{
    UIImage *image = mDefaultImage;

    if (image != NULL)
        image->draw(mImageDrawMode, mImageFlags, mX, mY, mColor);

    if (mSymbolImage != NULL)
    {
        INT thumbX = mX, thumbY = mY;
        if (mOrientation == HORIZONTAL)
            thumbX += mPosition - mSymbolImage->getWidth() / 2;
        else
            thumbY += mPosition - mSymbolImage->getHeight() / 2;
        mSymbolImage->draw(mSymbolImageDrawMode, mSymbolImageFlags, thumbX, thumbY, mItemColor);
    }
}
